package com.cloud.punish.tasklist.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.punish.common.result.Quokka;
import com.cloud.punish.common.result.SelectItem;
import com.cloud.punish.tasklist.bean.TaskListSearchField;
import com.cloud.punish.tasklist.entity.TaskListField;
import com.cloud.punish.tasklist.service.TaskListCacheService;
import com.cloud.punish.tasklist.service.TaskListFieldService;

@RestController
@RequestMapping("/punish/api/tasklist/fields")
public class TaskListFieldController {

	@Autowired
	private TaskListFieldService taskListFieldsService;

	@Autowired
	private TaskListCacheService taskListCacheService;

	@RequestMapping("/list")
	public Quokka listFields(long taskListId) {
		return new Quokka(true, taskListFieldsService.listFields(taskListId));
	}

	@RequestMapping("/update")
	public Quokka updateFields(@RequestBody List<TaskListField> fields) {
		return new Quokka(true, taskListFieldsService.updateFields(fields));
	}

	@GetMapping("/listsearch")
	public Quokka listSearchFields(long taskListId) {
		List<TaskListSearchField> searchFields = taskListFieldsService.listSearchFields(taskListId);
		for (TaskListSearchField searchField : searchFields) {
			if (!StringUtils.isEmpty(searchField.getDictKey())) {
				List<SelectItem> items = taskListCacheService.listSelectItems(searchField.getDictKey());
				items.add(0, new SelectItem("全部", ""));
				searchField.setItems(items);
			}
		}
		return new Quokka(true, searchFields);
	}

}
